//
//  UUIDManager.m
//  jyzx
//
//  Created by cyptt on 2022/6/13.
//

#import "CHUUIDManager.h"
#import "CHKeyChina.h"

static CHUUIDManager *_CHUUIDManager = nil;
@implementation CHUUIDManager

//static NSString * const KEY_IN_KEYCHAIN = @"项目的Bundle ID";
//static NSString * const KEY_UUID = @"项目的BundleID.uuid";
+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_CHUUIDManager == nil) {
            _CHUUIDManager = [[self alloc]init];
            
        }
    });
    return _CHUUIDManager;
}


-(NSString *)getBundId{
    if ( self.bundleId == nil || [self.bundleId isEqualToString:@""]) {
        return  NSBundle.mainBundle.bundleIdentifier;
    }else{
        return self.bundleId;
    }
}
-(NSString *)getUUIDkey{
    NSString * bundleId = [self getBundId];
    return [NSString stringWithFormat:@"%@_uuid",bundleId];
}


-(void)saveUUID:(NSString *)uuid{
    NSMutableDictionary *usernameUuidPairs = [NSMutableDictionary dictionary];
    [usernameUuidPairs setObject:uuid forKey:[self getUUIDkey]];
    [CHKeyChina save:self.bundleId data:usernameUuidPairs];
 
}
-(id)readUUID:(NSString *)uuid{
    NSMutableDictionary *usernameUuidPairs = (NSMutableDictionary *)[CHKeyChina load:self.bundleId];
    id objectResult = [usernameUuidPairs objectForKey:[self getUUIDkey]];
    NSLog(@"%@",[self getUUIDkey]);
    NSLog(@"%@",usernameUuidPairs);
    if ( objectResult == nil) {
        
        [self saveUUID:uuid];
        return uuid;
    }else{
        return [usernameUuidPairs objectForKey:[self getUUIDkey]];
    }
  
 
}
-(void)deleteUUID{
    [CHKeyChina deleteService:[self getBundId]];
}
@end
